package mirrormonkey.util.netevent.message.tools;

import java.util.LinkedList;
import java.util.List;

import junit.framework.Assert;

import com.jme3.network.Message;
import com.jme3.network.MessageConnection;
import com.jme3.network.MessageListener;

/**
 * Tracks received messages and provides methods for testing which messages have
 * been received.
 * 
 * @author Philipp Christian Loewner
 * 
 */
public class MockMessageListener implements MessageListener<MessageConnection> {

	public final List<MessageEvent> receivedEvents = new LinkedList<MessageEvent>();

	@Override
	public void messageReceived(MessageConnection source, Message m) {
		receivedEvents.add(new MessageEvent(source, m));
	}

	public void checkReceived(MessageConnection source, Message[] messages) {
		Assert.assertEquals(messages.length, receivedEvents.size());
		int i = 0;
		for (MessageEvent e : receivedEvents) {
			Assert.assertSame(source, e.source);
			Assert.assertSame(messages[i++], e.m);
		}
	}

	public static void checkReceived(MockMessageListener[] listeners,
			MessageConnection source, Message[] messages) {
		for (MockMessageListener l : listeners) {
			l.checkReceived(source, messages);
		}
	}

	public static void reset(MockMessageListener[] listeners) {
		for (MockMessageListener l : listeners) {
			l.receivedEvents.clear();
		}
	}

	public static class MessageEvent {
		public final MessageConnection source;

		public final Message m;

		public MessageEvent(MessageConnection source, Message m) {
			this.source = source;
			this.m = m;
		}

	}

}
